#include "Helpers/settingshelper.h"
#include <QFile>

SettingsHelper::SettingsHelper(QObject* parent) : QObject{parent} {}
SettingsHelper::~SettingsHelper() = default;

void SettingsHelper::init(char* argv[]) {
  auto applicationPath =
      QString::fromStdString(argv[0]);  // 默认第一个参数是程序路径
  const QFileInfo fileInfo(applicationPath);
  const QString iniFileName = fileInfo.completeBaseName() + ".ini";
  const QString iniFilePath =
      QStandardPaths::writableLocation(QStandardPaths::AppLocalDataLocation) +
      "/" + iniFileName;
  m_settings.reset(new QSettings(iniFilePath, QSettings::IniFormat));
}

void SettingsHelper::save(const QString& key, QVariant val) {
  m_settings->setValue(key, val);
}

QVariant SettingsHelper::get(const QString& key, QVariant def) {
  QVariant data = m_settings->value(key);
  if (!data.isNull() && data.isValid()) {
    return data;
  }
  return def;
}

bool SettingsHelper::setIntData(const QString& KeyName, const int& value) {
  try {
    save(KeyName, value);
  } catch (...) {
    qWarning() << "Error saving Data:" << KeyName << " " << value;
    return false;
  }

  return true;
}

int SettingsHelper::getIntData(const QString& KeyName, const int& defValue) {
  return get(KeyName, QVariant(defValue)).toInt();
}

bool SettingsHelper::setBoolData(const QString& KeyName, const bool& value) {
  try {
    save(KeyName, value);
  } catch (...) {
    qWarning() << "Error saving Data:" << KeyName << " " << value;
    return false;
  }

  return true;
}

bool SettingsHelper::getBoolData(const QString& KeyName, const bool& defValue) {
  return get(KeyName, QVariant(defValue)).toBool();
}

bool SettingsHelper::setDoubleData(const QString& KeyName,
                                   const double& value) {
  try {
    save(KeyName, value);
  } catch (...) {
    qWarning() << "Error saving Data:" << KeyName << " " << value;
    return false;
  }

  return true;
}

double SettingsHelper::getDoubleData(const QString& KeyName,
                                     const double& defValue) {
  return get(KeyName, QVariant(defValue)).toDouble();
}

bool SettingsHelper::setStringData(const QString& KeyName,
                                   const QString& value) {
  try {
    save(KeyName, value);
  } catch (...) {
    qWarning() << "Error saving Data:" << KeyName << " " << value;
    return false;
  }

  return true;
}

QString SettingsHelper::getStringData(const QString& KeyName,
                                      const QString& defValue) {
  return get(KeyName, QVariant(defValue)).toString();
}
